package com.ontheroad.mysql.scene.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.modoumama.service.base.impl.BaseServiceImpl;

import com.ontheroad.group.model.DeviceGroupEntity;
import com.ontheroad.group.model.LinkageGroupEntity;
import com.ontheroad.group.model.SceneGroupEntity;
import com.ontheroad.group.model.TimingGroupEntity;
import com.ontheroad.mysql.group.mapper.DeviceGroupMapper;
import com.ontheroad.mysql.group.mapper.LinkageGroupMapper;
import com.ontheroad.mysql.group.mapper.SceneGroupMapper;
import com.ontheroad.mysql.group.mapper.TimingGroupMapper;
import com.ontheroad.mysql.scene.mapper.CentralSceneMapper;
import com.ontheroad.mysql.scene.mapper.SceneMapper;
import com.ontheroad.scene.model.CentralSceneEntity;
import com.ontheroad.scene.model.SceneEntity;
import com.ontheroad.scene.service.SceneService;

/*import com.ontheroad.utils.SmsUtil;*/

/**
 * Created by kedong on 2017/6/25 0021.
 */
@Service
@Transactional
public class SceneServiceImpl extends BaseServiceImpl<SceneEntity, Integer>
		implements SceneService {

	private Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private CentralSceneMapper centralSceneMapper;

	@Autowired
	private SceneMapper sceneMapper;
	
	@Autowired
	private DeviceGroupMapper deviceGroupMapper;
	
	@Autowired
	private LinkageGroupMapper linkageGroupMapper;
	
	@Autowired
	private TimingGroupMapper timingGroupMapper;

	@Autowired
	private SceneGroupMapper sceneGroupMapper;

	public HashMap<String, Object> insertScene(Boolean isSub, int channel,
			String mac, SceneEntity sceneEntity) throws Exception {
		HashMap<String, Object> bean = new HashMap<String, Object>();
		log.info("添加场景信息");
		sceneEntity.setCreateTime(new Date());
		sceneEntity.setUpdateTime(new Date());
		sceneEntity.setIsEnable(true);
		sceneMapper.insertScene(sceneEntity);
		int id = sceneEntity.getId();
		sceneEntity.setId(id);
		CentralSceneEntity centralSceneEntity = new CentralSceneEntity();
		centralSceneEntity.setSceneId(id);
		centralSceneEntity.setIsSub(isSub);
		if (isSub == true) {
			centralSceneEntity.setChannel(channel);
		}
		centralSceneEntity.setMac(mac);
		centralSceneEntity.setUpdateTime(new Date());
		centralSceneEntity.setCreateTime(new Date());
		centralSceneEntity.setIsEnable(true);
		centralSceneMapper.insertCentralScene(centralSceneEntity);
		bean.put("code", 0);
		bean.put("message", "success");
		bean.put("isSub", isSub);
		bean.put("channel", channel);
		bean.put("mac", mac);
		bean.put("scene", sceneEntity);
		return bean;
	}

	public HashMap<String, Object> modifyScene(Boolean isSub, int channel,
			String mac, SceneEntity sceneEntity) throws Exception {
		HashMap<String, Object> bean = new HashMap<String, Object>();
		log.info("修改场景信息");
//		CentralSceneEntity centralSceneEntity = new CentralSceneEntity();
//		centralSceneEntity.setIsSub(isSub);
//		if (isSub == true) {
//			centralSceneEntity.setChannel(channel);
//		}
//		centralSceneEntity.setMac(mac);
//		Integer sceneId = centralSceneMapper.findSceneId(centralSceneEntity);
//		sceneEntity.setId(sceneId);
		sceneMapper.modifyScene(sceneEntity);
		bean.put("code", 0);
		bean.put("message", "success");
		bean.put("isSub", isSub);
		bean.put("channel", channel);
		bean.put("mac", mac);
		bean.put("scene", sceneEntity);
		return bean;
	}

	public HashMap<String, Object> deleteScene(Boolean isSub, int channel,
			String mac, SceneEntity sceneEntity) throws Exception {
		HashMap<String, Object> bean = new HashMap<String, Object>();
		log.info("删除场景信息");
//		CentralSceneEntity centralSceneEntity = new CentralSceneEntity();
//		centralSceneEntity.setIsSub(isSub);
//		if (isSub == true) {
//			centralSceneEntity.setChannel(channel);
//		}
//		centralSceneEntity.setMac(mac);
//		Integer sceneId = centralSceneMapper.findSceneId(centralSceneEntity);
//		sceneEntity.setId(sceneId);
		sceneEntity.setIsEnable(false);
		sceneMapper.modifyScene(sceneEntity);
		bean.put("code", 0);
		bean.put("message", "success");
		bean.put("isSub", isSub);
		bean.put("channel", channel);
		bean.put("mac", mac);
		bean.put("scene", sceneEntity);
		return bean;
	}

	public HashMap<String, Object> findScenes(Boolean isSub, int channel,
			String mac) throws Exception {
		HashMap<String, Object> bean = new HashMap<String, Object>();
		log.info("获取场景列表");
		CentralSceneEntity centralSceneEntity = new CentralSceneEntity();
		centralSceneEntity.setIsSub(isSub);
		if (isSub == true) {
			centralSceneEntity.setChannel(channel);
		}
		centralSceneEntity.setMac(mac);
		List<Integer> sceneIds = centralSceneMapper.findSceneId(centralSceneEntity);
		List<SceneEntity> seneEntityList=sceneMapper.findSceneList(sceneIds);
		bean.put("code", 0);
		bean.put("message", "success");
		bean.put("isSub", isSub);
		bean.put("channel", channel);
		bean.put("mac", mac);
		bean.put("scene", seneEntityList);
		return bean;
	}

	public HashMap<String, Object> findScenesInfo(int sceneId) throws Exception {
		HashMap<String, Object> bean = new HashMap<String, Object>();
		log.info("获取场景信息");
		List<SceneGroupEntity> sceneGroupEntityList=sceneGroupMapper.findModelById(sceneId);
		List<Integer> timingList=new ArrayList<Integer>();
		List<Integer> deviceList=new ArrayList<Integer>();
		List<Integer> LinkageList=new ArrayList<Integer>();
		for(SceneGroupEntity sceneGroupEntity: sceneGroupEntityList){
			if(sceneGroupEntity.getTimingGroupId()!=null){
				timingList.add(sceneGroupEntity.getTimingGroupId());
			}
			if(sceneGroupEntity.getDeviceGroupId()!=null){
				deviceList.add(sceneGroupEntity.getDeviceGroupId());
			}
			if(sceneGroupEntity.getLinkageId()!=null){
				LinkageList.add(sceneGroupEntity.getLinkageId());
			}
		}
		List<TimingGroupEntity> TimingGroupEntityList=timingGroupMapper.findModelByIds(timingList);
		List<DeviceGroupEntity> DeviceGroupEntityList=deviceGroupMapper.findModelByIds(deviceList);
		List<LinkageGroupEntity> LinkageGroupEntityList=linkageGroupMapper.findModelByIds(LinkageList);
		bean.put("timings", TimingGroupEntityList);
		bean.put("devices", DeviceGroupEntityList);
		bean.put("scene", sceneId);
		bean.put("linkages", LinkageGroupEntityList);
		return bean;
	}

}
